﻿using System;
using System.Globalization;
using System.Windows.Data;
using FFmpegVideoClip.Configuration;
using FFmpegVideoClip.Mode;

namespace FFmpegVideoClip.Lib.UtilityControlsTools
{
    [ValueConversion(typeof(object[]), typeof(string))]
    internal class ProgressRatioToTimeRemainingConvert : IMultiValueConverter
    {
        private record struct ProgressRecord(DateTime Time,double Offset);
        private static TimeSpan SpanTime = Config.RemainingTimeStatisticsDurationValue;
        private static DateTime CurrentTime, NextTime, StartTime;
        private static ProgressRecord CurrentRecord, CacheRecord;
        
        public static void Init()
        {
            StartTime = DateTime.Now;
            SpanTime = Config.RemainingTimeStatisticsDurationValue;
            CacheRecord = CurrentRecord = new(CurrentTime = StartTime, 0);
            NextTime = CurrentRecord.Time + SpanTime;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return IntervalConvert(values,targetType,parameter);
            }
            catch (Exception exc)
            {
               LogManage.WriteExceptionInfo($"进度转换异常:[Max:{values[0]}][Value:{values[1]}]",exc,false);
               return $"进度转换异常:[Max:{values[0]}][Value:{values[1]}]";
            }
        }

        private static object IntervalConvert(object[] values, Type targetType, object parameter)
        {
            //编辑模式下，始终非法
            if (!ProgressBarToStringConvert.VerifyParametersValidity(values))
                return "参数非法";
            double Max = (double)values[0], Value = (double)values[1];
            if (Value < 0.0005d)
                return $"预计剩余时间:...{Value}";
            double Ratio = (Value / Max).GetValid();
            CurrentTime = DateTime.Now;
            if (CurrentTime > NextTime)
            {
                NextTime = CurrentTime + SpanTime;
                CurrentRecord = CacheRecord;
                CacheRecord = new ProgressRecord(CurrentTime, Ratio);
                LogManage.WriteLineNewInfo($"时间:{CurrentRecord.Time} 进度:{CurrentRecord.Offset}");
            }

            double CurrentValue = Ratio - CurrentRecord.Offset;
            ProgressStatistics.ProgressValue CompletedCount = ProgressStatistics.Instance.CompletedCountProgressValue;
            return $"完成数统计[{Math.Round(CompletedCount.RatioValue * 100, 2, MidpointRounding.AwayFromZero).GetValid()}%][{CompletedCount.Progress_Value}/{CompletedCount.TotalValue}]" +
                $"预计剩余时间(全程):[{Tools.GetTimeSpanToString_HoursMinutesSeconds((CurrentTime - StartTime) * (1 - Ratio) / Ratio)}] - " +
            $"预计剩余时间(时段):[{Tools.GetTimeSpanToString_HoursMinutesSeconds((CurrentTime - CurrentRecord.Time) * (1 - CurrentRecord.Offset - CurrentValue) / CurrentValue)}]";

        }



        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
